//
//  HMNetworkTools.swift
//  02-AFN网络封装
//
//  Created by apple on 16/8/10.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import AFNetworking
// 网络请求枚举
enum HMNetworkToolsMethod: String {
    // GET 请求
    case GET = "GET"
    // POST 请求
    case POST = "POST"
}
/*
    - 对afn进行封装
    - 提供一个可以发送get和post请求的公共方法
    - 帮助当前app 请求所有接口数据
*/
class HMNetworkTools: AFHTTPSessionManager {
    // 全局访问点
    static let sharedTools:HMNetworkTools = {
        let tools = HMNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    /**
     网络请求公共方法
     
     - parameter method:     请求方式
     - parameter urlString:  请求urlString
     - parameter parameters: 请求参数
     - parameter success:    请求成功的闭包
     - parameter failure:    请求失败的闭包
     */
    func request(method: HMNetworkToolsMethod, urlString: String, parameters: AnyObject?, success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        // get请求
        if method == .GET {
            GET(urlString, parameters: parameters, progress: nil, success: { (_, response) -> Void in
                // 03
                success(response: response)
                }, failure: { (_, error) -> Void in
                    // 03
                  failure(error: error)
            })
            
        }else {
            // post 请求
            POST(urlString, parameters: parameters, progress: nil, success: { (_, response) -> Void in
                // 03
                success(response: response)
                }, failure: { (_, error) -> Void in
                    // 03
                    failure(error: error)
            })
            
        }
        
    }
 
}
//MARK: - 发布微博界面请求
extension HMNetworkTools{
    // 发送文字微博
    func composeUpdate(status: String, success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        // 请求的url
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        // 请求参数
        let params = [
            "access_token": HMUserAccountViewModel.sharedTools.accessToken!,
            "status": status
        ]
        // 发送请求
        request(HMNetworkToolsMethod.POST, urlString: urlString, parameters: params, success: success, failure: failure)
    }
    // 发布图片微博
    func composeUpload(status: String, images:[UIImage] , success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        // 请求的urlString
        let urlString = "https://upload.api.weibo.com/2/statuses/upload.json"
        // 请求参数
        let params = [
            "access_token": HMUserAccountViewModel.sharedTools.accessToken!,
            "status": status
        ]
        
        // 发送请求
        POST(urlString, parameters: params, constructingBodyWithBlock: { (formdata) -> Void in
            // 二进制
            let data = UIImagePNGRepresentation(images.first!)!
            formdata.appendPartWithFileData(data, name: "pic", fileName: "yxyx", mimeType: "application/octet-stream")
            }, progress: nil, success: { (_, response) -> Void in
                success(response: response)
            }) { (_, error) -> Void in
                failure(error: error)
        }

    }
}

//MARK: - 微博首页数据
extension HMNetworkTools{
    func homeLoadData(sinceId:Int64, maxId:Int64,  success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        // 请求的urlString
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        // 请求参数
        let params = [
            "access_token": HMUserAccountViewModel.sharedTools.userAccount!.access_token!,
            "since_id":"\(sinceId)",
            "max_id":"\(maxId)"
        ]

        request(HMNetworkToolsMethod.GET, urlString: urlString, parameters: params, success: success, failure: failure)
        
    }
}
//MARK: - 授权界面接口
extension HMNetworkTools {
    // 请求授权
    func oauthLoadUserAccount(code: String, success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        // 请求urlString
        let urlString = "https://api.weibo.com/oauth2/access_token"
        // 请求参数
        let params = [
            "client_id":WBAppKey,
            "client_secret": WBAppSecret,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri": WBRedirect_uri
        ]
        
        request(HMNetworkToolsMethod.POST, urlString: urlString, parameters: params, success: success, failure: failure)
    }
    
    // 请求个人信息
    func oauthLoadUserInfo(userAccount: HMUserAccountModel, success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        // 请求urlString
        let urlString = "https://api.weibo.com/2/users/show.json"
        // 请求参数
        let params = [
            "access_token":userAccount.access_token!,
            "uid":userAccount.uid!
        ]
        
        request(HMNetworkToolsMethod.GET, urlString: urlString, parameters: params, success: success, failure: failure)
    }
}

